package com.profile.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.profile.domain.Profile;
import com.profile.domain.SearchCriteria;
import com.profile.service.ProfileService;
import com.profile.service.SkillService;
import com.profile.service.TittleService;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping({"/","/home.html"})
public class HomeController {

	@Autowired
	private ProfileService profileService;

	@Autowired
	private SkillService skillService ;
	
	@Autowired
	private TittleService tittleService;
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String home(Model model) {
		model.addAttribute("profiles", profileService.getAllProfiles());
		model.addAttribute("searchCriteria", new SearchCriteria());
		model.addAttribute("listTittles",tittleService.GetAllTittles());
		model.addAttribute("listSkills", skillService.GetAllSkill());
		return "home";
	}
	
	@RequestMapping(params = "delete", method = RequestMethod.POST)
	public String delete(@RequestParam(value = "id", required = true) int ID) {
		profileService.deleteProfile(ID);
		return "redirect:home.html";
	}
	
	@RequestMapping(params = "search", method = RequestMethod.POST)
	public String search( SearchCriteria searchCriteria , BindingResult result, SessionStatus status, ModelMap model){
		model.addAttribute("profiles", profileService.searchProfiles(searchCriteria));
		model.addAttribute("listTittles",tittleService.GetAllTittles());
		model.addAttribute("listSkills", skillService.GetAllSkill());
		return "home";
	}
	
}
